import matplotlib.pyplot as plt
import numpy as np

# --- 全局设置，用于正确显示中文和负号 ---
# Matplotlib 在 Windows 上默认可能不支持中文，需要设置字体
# SimHei 是一个常用的支持中文的黑体字
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False # 解决负号'-'显示为方块的问题

def generate_line_chart():
    """生成折线图"""
    fig, ax = plt.subplots()
    days = [1, 2, 3, 4, 5, 6, 7]
    hours = [1, 2.5, 3, 4, 3.5, 5, 4.5]
    ax.plot(days, hours, marker='o', linestyle='--')
    ax.set_title("一周学习时长变化")
    ax.set_xlabel("天数")
    ax.set_ylabel("学习小时数")
    ax.grid(True) # 添加网格线
    plt.show()  # 显示图表
    # plt.savefig("line_chart1.png")
    plt.close(fig)

def generate_bar_chart():
    """生成柱状图"""
    fig, ax = plt.subplots()
    fruits = ['苹果', '香蕉', '橙子', '草莓']
    sales = [50, 80, 45, 120]
    ax.bar(fruits, sales, color=['red', 'yellow', 'orange', 'pink'])
    ax.set_title("本周水果销量")
    ax.set_xlabel("水果种类")
    ax.set_ylabel("销量 (箱)")
    plt.savefig("bar_chart.png")
    plt.close(fig)

def generate_scatter_plot():
    """生成散点图"""
    fig, ax = plt.subplots()
    # 模拟一些身高和体重数据
    height = np.random.normal(170, 10, 100)
    weight = height - 105 + np.random.normal(0, 5, 100)
    ax.scatter(height, weight, alpha=0.6)
    ax.set_title("身高与体重关系散点图")
    ax.set_xlabel("身高 (cm)")
    ax.set_ylabel("体重 (kg)")
    plt.savefig("scatter_plot.png")
    plt.close(fig)

def generate_histogram():
    """生成直方图"""
    fig, ax = plt.subplots()
    # 模拟一个班级100名学生的考试成绩
    scores = np.random.normal(75, 15, 100)
    ax.hist(scores, bins=10, edgecolor='black')
    ax.set_title("学生考试成绩分布直方图")
    ax.set_xlabel("分数")
    ax.set_ylabel("学生人数")
    plt.savefig("histogram.png")
    plt.close(fig)

def generate_pie_chart():
    """生成饼图"""
    fig, ax = plt.subplots()
    labels = '学习', '娱乐', '吃饭', '睡觉'
    sizes = [40, 20, 15, 25]
    explode = (0.1, 0, 0, 0)  # "explode" the 1st slice (i.e. '学习')
    ax.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
           shadow=True, startangle=90)
    ax.axis('equal')  # 保证饼图是圆的
    ax.set_title("一天时间分配饼图")
    plt.savefig("pie_chart.png")
    plt.close(fig)

def generate_box_plot():
    """生成箱形图"""
    fig, ax = plt.subplots()
    # 模拟三个不同班级的数学成绩
    class1_scores = np.random.normal(70, 10, 50)
    class2_scores = np.random.normal(75, 15, 50)
    class3_scores = np.random.normal(80, 5, 50)
    data = [class1_scores, class2_scores, class3_scores]
    ax.boxplot(data, labels=['一班', '二班', '三班'])
    ax.set_title("不同班级数学成绩箱形图")
    ax.set_ylabel("分数")
    plt.savefig("box_plot.png")
    plt.close(fig)

def generate_contour_plot():
    """生成等高线图"""
    fig, ax = plt.subplots()
    # 创建网格数据
    x = np.linspace(-3.0, 3.0, 100)
    y = np.linspace(-3.0, 3.0, 100)
    X, Y = np.meshgrid(x, y)
    Z = np.exp(-(X**2 + Y**2))
    
    # 绘制等高线
    contour = ax.contour(X, Y, Z, colors='black')
    ax.clabel(contour, inline=True, fontsize=8)
    
    # 绘制填充的等高线
    contourf = ax.contourf(X, Y, Z, cmap='viridis')
    fig.colorbar(contourf, ax=ax) # 添加颜色条
    
    ax.set_title('等高线图示例')
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    plt.savefig("contour_plot.png")
    plt.close(fig)


if __name__ == '__main__':
    print("开始生成所有图表...")
    generate_line_chart()
    generate_bar_chart()
    generate_scatter_plot()
    generate_histogram()
    generate_pie_chart()
    generate_box_plot()
    generate_contour_plot()
    print("所有图表已成功生成并保存为图片文件！")
